package com.example.screenprojection;

import android.content.Intent;
import android.media.projection.MediaProjectionManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
private static final int PROJECTION_REQUEST_CODE = 1;

private MediaProjectionManager mediaProjectionManager;
private String mScreenServiceDevices;
private List<String> mScreenServiceDevicesMutableList = new ArrayList<>();

@Override
protected void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();

        MdnsService mdnsService = new MdnsService();
        mdnsService.startMdnsService();
        EventBus.getDefault().register(this);



}

private void init () {
        mediaProjectionManager = (MediaProjectionManager) getSystemService(MEDIA_PROJECTION_SERVICE);
}


public void onClick (View view) {
        if (view.getId() == R.id.btn_start) {
                startProjection();
        } else if ((view.getId() == R.id.btn_search_devices)) {
                // 启动服务
                Intent intent = new Intent(this, UDPBroadcastService.class);
                startService(intent);

        }
}

// 请求开始录屏
private void startProjection () {
        Intent intent = mediaProjectionManager.createScreenCaptureIntent();
        startActivityForResult(intent, PROJECTION_REQUEST_CODE);
}

@Override
protected void onActivityResult (int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK) {
                return;
        }
        if (requestCode == PROJECTION_REQUEST_CODE) {
                Intent service = new Intent(this, ScreenService.class);
                service.putExtra("code", resultCode);
                service.putExtra("data", data);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                        startForegroundService(service);
                } else {
                        startService(service);
                }
        }
}

@Override
protected void onDestroy () {
        super.onDestroy();
        // 停止服务
        Intent intent = new Intent(this, UDPBroadcastService.class);
        stopService(intent);

}

@Subscribe(threadMode = ThreadMode.BACKGROUND)
public void onGetMessage (ScreeDevices data) {
        Log.d("xxxx", "message=========>" + data.toString());

        if (!data.message.equals(mScreenServiceDevices)) {
                mScreenServiceDevices = data.message;  // 更新上次接收到的消息
                addDevice(mScreenServiceDevices);
        } else {
                Log.d("xxxx", "接收到重复的广播信息，不发送");
        }
}

public void addDevice (String device) {



        if (!mScreenServiceDevicesMutableList.contains(device)) {
                mScreenServiceDevicesMutableList.add(device);
                // 初始化数据源
                mScreenServiceDevicesMutableList = new ArrayList<>();
                for (int i = 0; i < 20; i++) {
                        mScreenServiceDevicesMutableList.add("Item " + (i + 1));
                }
        }
}

}

